package com.lisx.module_time_block.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.lib_data.constants.EventBusConstants;
import com.fenghuajueli.lib_data.entity.db.DayPlanDbEntity;
import com.fenghuajueli.lib_data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.manager.TimeBlockManager;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.libbasecoreui.utils.MyStatusBarUtils;
import com.lisx.module_time_block.R;
import com.lisx.module_time_block.adapter.NewBlockColorAdapter;
import com.lisx.module_time_block.adapter.NewBlockIconAdapter;
import com.lisx.module_time_block.bean.NewBlockColorBean;
import com.lisx.module_time_block.bean.NewBlockIconBean;
import com.lisx.module_time_block.databinding.ActivityNewBlockBinding;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NewBlockActivity extends BaseActivity {

    private ActivityNewBlockBinding binding;
    int iconPosition;
    int colorPosition;
    private List<NewBlockIconBean> iconData;
    private List<NewBlockColorBean> colorData;
    private int index;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNewBlockBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        MyStatusBarUtils.setStatusBar(this,"#5AB4C7");
        index = getIntent().getIntExtra("index", 0);

        iconData = NewBlockIconBean.getNewBlockIconData();
        binding.iconRecyclerview.setLayoutManager(new GridLayoutManager(this, 6));
        NewBlockIconAdapter iconAdapter = new NewBlockIconAdapter(iconData);
        binding.iconRecyclerview.setAdapter(iconAdapter);
        iconAdapter.setOnItemClickListener(new NewBlockIconAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                iconPosition = position;
            }
        });

        colorData = NewBlockColorBean.getNewBlockColorData();
        binding.colorRecyclerview.setLayoutManager(new GridLayoutManager(this, 6));
        NewBlockColorAdapter colorAdapter = new NewBlockColorAdapter(this, colorData);
        binding.colorRecyclerview.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(new NewBlockColorAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                colorPosition = position;
            }
        });

        binding.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveBlock();
            }
        });
    }

    private void saveBlock() {
        String name = binding.etName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort("请给该习惯命名!");
            return;
        }

        DayPlanDbEntity entity = new DayPlanDbEntity();
        entity.setPlan_name(name);
//        entity.setRes(iconData.get(iconPosition).res);
        //保存position
        entity.setResPosition(iconPosition);
        entity.setColor(colorData.get(colorPosition).color);
        entity.setCreate_time(System.currentTimeMillis());
        entity.setDaka_count(0);
        entity.setType(index);
        Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Boolean> emitter) throws Exception {
                TimeBlockManager.getInstance().saveDayPlanDbEntity(entity);
                emitter.onNext(true);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Boolean aBoolean) {
                        EventBus.getDefault().post(new EventEntity(EventBusConstants.SAVE_PLAN));
                        ToastUtils.showShort("保存成功");
                        finish();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
